<?php

namespace App\Http\Resources;

use App\Models\Order;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class OrderDetailResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'type' => $this->type,
            'order_sn' => $this->order_sn,
            'freight_price' => $this->freight_price,
            'coupon_price' => $this->coupon_price,
            'total_amount' => $this->total_amount,
            'created_at' => Carbon::parse($this->created_at)->toDateTimeString(),
            'ship_status_name' => Order::$shipStatusMap[$this->ship_status],
            'ship_status' => $this->getAttribute('ship_status'),
            'closed' => $this->closed,
            'address' => $this->address,
            'out_time' => $this->out_time,
            'shared_member_id' => $this->shared_member_id,
            'remark' => $this->remark,
            'items' => OrderItemsResource::collection($this->whenLoaded('items'))
        ];
    }
}
